home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / MacODBC / ODBC Tools / SampleApp / SampleApp.c < prev   
Encoding:
C/C++ Source or Header  |  1995-05-01  |  5.3 KB  |  233 lines  |  [TEXT/MPS ]

  1. #define MACODBC 1
  2.  
  3. #if ! PPCODBC
  4. #include "LibraryManager.h"
  5. #endif
  6.  
  7. #include <Types.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. #include "sqlext.h"
  12.  
  13. void quickTest();
  14.  
  15. #include <OSUtils.h>
  16.  
  17. main()
  18. {    
  19.     fprintf( stdout, "ear wig oh\n" ); fflush( stdout );
  20. //    SysBeep(10);
  21.  
  22. #if ! PPCODBC
  23.     InitLibraryManager(0, kCurrentZone, kNormalMemory);
  24. #endif
  25.  
  26.     quickTest();
  27.  
  28. #if ! PPCODBC
  29.     CleanupLibraryManager();
  30. #endif
  31.  
  32.     fprintf( stdout,  "cya\n" ); fflush( stdout );
  33.     
  34.     return 0;
  35. }
  36.  
  37.  
  38. //____ quickTest stuff _________________________
  39.  
  40. #include "autotests.h"
  41.  
  42. BOOL_EXTFUNCDECL_FAR szLogPrintf(lpSERVERINFO lps, BOOL fForce, LPSTR szFmt /*, ...*/)
  43. {
  44. #pragma unused(lps)
  45.  
  46.     fprintf( stdout, szFmt );
  47.     fflush( stdout );
  48.     
  49.     return 0;
  50. }
  51.  
  52. #include <events.h>
  53. #include <toolutils.h>
  54.  
  55. #define KEY_CONTROL 59
  56. #define KEY_OPTION 58
  57. #define KEY_SHIFT 56
  58. #define KEY_COMMAND 55
  59.  
  60. Boolean        KeyIsDown(
  61.     short            theKeyCode)
  62. {
  63.     KeyMap            theKeys;
  64.     
  65.     GetKeys(theKeys);                    /* Get state of each key            */
  66.                                         
  67.         /* Ordering of bits in a KeyMap is truly bizarre. A KeyMap is a    */
  68.         /* 16-byte (128 bits) array where each bit specifies the start    */
  69.         /* of a key (0 = up, 1 = down). We isolate the bit for the        */
  70.         /* specified key code by first determining the byte position in    */
  71.         /* the KeyMap and then the bit position within that byte.        */
  72.         /* Key codes 0-7 are in the first byte (offset 0 from the        */
  73.         /* start), codes 8-15 are in the second, etc. The BitTst() trap    */
  74.         /* counts bits starting from the high-order bit of the byte.    */
  75.         /* For example, for key code 58 (the option key), we look at    */
  76.         /* the 8th byte (7 offset from the first byte) and the 5th bit    */
  77.         /* within that byte.                                            */
  78.         /* p.s. - key 59 is ? */
  79.         
  80.     return( BitTst( ((char*) &theKeys) + theKeyCode / 8,
  81.                     (long) 7 - (theKeyCode % 8) ) );
  82. }
  83. #include <String.h>
  84.  
  85. void 
  86. quickTest()
  87. {
  88.     SERVERINFO    si;
  89.     char buf[ 100 ];
  90.  
  91. again:    
  92.     memset(&si, 0, sizeof(si));
  93.     
  94.     si.hwnd = NULL; //pTestSource->pGrafPort;
  95.     si.henv = NULL;
  96.     si.hdbc = NULL;
  97.     
  98.     strcpy(si.szSource, "");
  99.     printf( "data source: " ); fflush( stdout );
  100.     gets( buf );
  101.     strcpy(si.szValidServer0, buf );
  102.     printf( "login: " ); fflush( stdout );
  103.     gets( buf );
  104.     strcpy(si.szValidLogin0, buf );
  105.     printf( "password: " ); fflush( stdout );
  106.     gets( buf );
  107.     strcpy(si.szValidPassword0, buf);
  108.  
  109.     if ( KeyIsDown( KEY_OPTION ) ) // option
  110.     {
  111.         si.szBuff[0] = 1;
  112.         SysBeep( 10 );
  113.     }
  114.     
  115.     fprintf( stdout, "%s - %s - %s\n", si.szValidServer0, si.szValidLogin0, si.szValidPassword0 ); fflush( stdout );
  116. //    si.rglMask = (UINT) pTestSource->rglMask;
  117.  
  118.     AutoTestFunc(&si);
  119.     
  120.     printf( "again? " ); fflush( stdout );
  121.     gets( buf );
  122.     if ( *buf == 'y' ) { goto again; }
  123.  
  124. }
  125.  
  126.  
  127. #if 0
  128. //____ cursTest stuff _________________________
  129.  
  130. HENV henv = 0;
  131. HDBC hdbc = 0;
  132.  
  133. void
  134. cc( RETCODE rc, char* fn, HSTMT hstmt = NULL )
  135. {
  136.     if ( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO )
  137.     {
  138.         fprintf( stdout, "%s failed (%d)\n", fn, rc );
  139.  
  140.         UCHAR state[6];
  141.         SDWORD nativeErr;
  142.         UCHAR msg[200];
  143.         SWORD msglen;
  144.         RETCODE rc;
  145.         
  146.         for (;;)
  147.         {
  148.             rc = SQLError( henv, hdbc, hstmt, state, & nativeErr, msg, 200, & msglen );
  149.             if ( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO ) { break; }
  150.             fprintf( stdout, "   %s: %s (%ld)\n", state, msg, nativeErr );
  151.         }
  152.         fflush( stdout );
  153.         
  154.         if ( hdbc ) 
  155.         {
  156.             SQLDisconnect( hdbc ); 
  157.             SQLFreeConnect( hdbc ); 
  158.         }
  159.         if ( henv ) { SQLFreeEnv( henv ); }
  160.         exit( -1 );
  161.     }
  162. }
  163.  
  164.  
  165. typedef unsigned char * UCP;
  166.  
  167. const UDWORD rows = 2;
  168. const SDWORD fnmmax = 30;
  169. const SDWORD lnmmax = 30;
  170.  
  171. void
  172. cursTest()
  173. {
  174.     cc( SQLAllocEnv( & henv ), "SQLAllocEnv" );
  175.     cc( SQLAllocConnect( henv, & hdbc ), "SQLAllocConnect" );
  176.  
  177. #if ! PPCODBC
  178.     cc( SQLSetConnectOption( hdbc, SQL_ODBC_CURSORS, SQL_CUR_USE_ODBC ), "SQLSetConnectOption" );
  179. #endif
  180.     cc( SQLConnect( hdbc, (UCP)"foo Data Source", SQL_NTS, (UCP)"", SQL_NTS, (UCP)"", SQL_NTS ),  "SQLConnect" );
  181.  
  182.     HSTMT s1, s2;
  183.     cc( SQLAllocStmt( hdbc, & s1 ), "alloc s1" );
  184.     cc( SQLAllocStmt( hdbc, & s2 ), "alloc s2" );
  185.  
  186. #if ! PPCODBC // temporarily zap cursor lib stuff on PPC    
  187.     cc( SQLSetStmtOption( s1, SQL_CONCURRENCY, SQL_CONCUR_VALUES ), "s1 opt", s1 );
  188.     cc( SQLSetStmtOption( s1, SQL_CURSOR_TYPE, SQL_CURSOR_STATIC ), "s1 opt2", s1 );
  189.     cc( SQLSetStmtOption( s1, SQL_ROWSET_SIZE, rows ), "s1 opt3", s1 );
  190.     cc( SQLSetCursorName( s1, (UCP)"ECURS", SQL_NTS ), "s1 cursnm", s1 );
  191.   
  192.     cc( SQLExecDirect( s1, (UCP)"SELECT FIRST_NAME, LAST_NAME FROM EMPLOYEE", SQL_NTS ),
  193.             "SQLExecDirect", s1 );
  194.     
  195.     UCHAR fnm[ rows ][ fnmmax ], lnm[ rows ][ lnmmax ];
  196.     SDWORD fnmlen[ rows ], lnmlen[ rows ];
  197.     cc( SQLBindCol( s1, 1, SQL_C_CHAR, fnm, fnmmax, fnmlen ), "s1 bind 1", s1 );
  198.     cc( SQLBindCol( s1, 2, SQL_C_CHAR, lnm, lnmmax, lnmlen ), "s1 bind 2", s1 );
  199.     
  200.     UWORD rowstatus[ rows ];
  201.     UDWORD crow;
  202.     cc( SQLExtendedFetch( s1, SQL_FETCH_ABSOLUTE, - rows, & crow, rowstatus ),
  203.             "fetch last", s1 );
  204.     
  205.     while ( crow )
  206.     {
  207.         --crow;
  208.         fprintf( stdout,  "%s %s\n", fnm[ crow ], lnm[ crow ] );
  209.     }
  210.     fprintf( stdout,  "\n" ); fflush( stdout );
  211.     
  212.     for (;;)
  213.     {
  214.         RETCODE rc = SQLExtendedFetch( s1, SQL_FETCH_PRIOR, 1, & crow, rowstatus );
  215.         if ( rc == SQL_NO_DATA_FOUND ) { break; }
  216.         cc( rc, "fetch previous", s1 );
  217.         while ( crow )
  218.         {
  219.             --crow;
  220.             fprintf( stdout,  "%s %s\n", fnm[ crow ], lnm[ crow ] );
  221.         }
  222.         fprintf( stdout,  "\n" ); fflush( stdout );
  223.     }
  224. #endif
  225.  
  226. cleanup:
  227.     cc( SQLDisconnect( hdbc ), "SQLDisconnect" ); 
  228.     cc( SQLFreeConnect( hdbc ), "SQLFreeConnect" ); 
  229.     hdbc = 0;
  230.     cc( SQLFreeEnv( henv ), "SQLFreeEnv" ); 
  231. }
  232.  
  233. #endif //0